﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SystemDirectory.Common;
using SystemDirectory.Entity;
using SystemDirectory.Biz;
using System.Data;

namespace SystemDirectory
{
    public partial class ContactEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BOContact boContact = new BOContact();
                Contact contact = new Contact();
                contact = boContact.GetById(Convert.ToInt32(Request.QueryString["id"].ToString()));

                lbnModal.Attributes.Add("data-toggle", "modal");
                lbnModal.Attributes.Add("onclick", "btnModalClick()");
                contact_type.Items.Add(new ListItem("Operational", "0"));
                contact_type.Items.Add(new ListItem("Fire marshall", "1"));

                contact_method.Items.Add(new ListItem("method 01", "0"));
                contact_method.Items.Add(new ListItem("method 02", "1"));
                contact_method.Items.Add(new ListItem("method 03", "2"));

                txtId.Text = contact.ContactId.ToString();
                manager_name.Text = contact.ManageId.ToString();
                first_name.Text = contact.FirstName;
                sur_name.Text = contact.SurName;
                known_as.Text = contact.KnownAs;
                office_phone.Text = contact.OfficePhone;
                mobile_phone.Text = contact.MobilePhone;
                home_phone.Text = contact.STHomePhone;
                email.Text = contact.Email;
                contact_type.SelectedValue = contact.ContactType.ToString();
                job_role.Text = contact.JobRole;
                workbase.Text = contact.WorkBase;
                job_title.Text = contact.JobTitle;
                is_active.Checked = contact.IsActive;
                


                DataTable dtContact = boContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);
                rptContact.DataSource = dtContact;
                rptContact.DataBind();

                //modalLookup.Visible = false;
                wrap.Visible = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Contact cttNew = new Contact();
            cttNew.ContactId = Convert.ToInt32(txtId.Text);
            cttNew.ManageId = Convert.ToInt32(manager_name.Text);
            cttNew.FirstName = first_name.Text;
            cttNew.SurName = sur_name.Text;
            cttNew.KnownAs = known_as.Text;
            cttNew.OfficePhone = office_phone.Text;
            cttNew.MobilePhone = mobile_phone.Text;
            cttNew.STHomePhone = home_phone.Text;
            cttNew.Email = email.Text;
            cttNew.ContactType = Convert.ToInt32(contact_type.SelectedValue);
            cttNew.JobRole = job_role.Text;
            cttNew.WorkBase = workbase.Text;
            cttNew.JobTitle = job_title.Text;
            cttNew.IsActive = is_active.Checked;

            BOContact boContact = new BOContact();
            if (boContact.Update(cttNew))
            {
                Response.Redirect("/Pages/Views/ContactList.aspx");
            }
        }

        protected void cbxInactive_CheckedChanged(object sender, EventArgs e)
        {
            BOContact buContact = new BOContact();
            DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);
            rptContact.DataSource = dtContact;
            rptContact.DataBind();
            wrap.Visible = true;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BOContact buContact = new BOContact();
            DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);
            rptContact.DataSource = dtContact;
            rptContact.DataBind();
            wrap.Visible = true;
        }

        protected void lbnModal_Click(object sender, EventArgs e)
        {
            wrap.Visible = true;
        }
    }
}